﻿using QuanLyGiaoVuDAL.DTO;
using QuanLyGiaoVuDAL.Repositories.DatabaseRepositories;
using QuanLyGiaoVuService.CommonServices;
using QuanLyGiaoVuService.Domain;
using QuanLyGiaoVuService.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QuanLyGiaoVuService.ReportServices
{
    public class LopMonHocReportService : ILopMonHocReportService
    {
        public ILopMonHocRepository _lopMonHocRepository;
        private IDangKyGioDayRepository _dangKyGioDayRepository;

        public LopMonHocReportService(ILopMonHocRepository lopMonHocRepository, IDangKyGioDayRepository dangKyGioDayRepository)
        {
            _lopMonHocRepository = lopMonHocRepository;
            _dangKyGioDayRepository = dangKyGioDayRepository;
        }

        public QuanLyGiaoVuDAL.DTO.LopMonHocDTO GetLopMonHocById(Guid id)
        {
            return _lopMonHocRepository.GetLopMonHocById(id);
        }

        public IEnumerable<QuanLyGiaoVuDAL.DTO.LopMonHocDTO> GetAllLopMonHoc()
        {
            return _lopMonHocRepository.GetAllLopMonHoc(null,null,null,null, null);
        }

        public IEnumerable<QuanLyGiaoVuDAL.DTO.LopMonHocDTO> GetAllLopMonHocTheoHocKy(Guid maHocKy, bool? giangVienXacNhan, bool? daPhanHoi, bool? dongYMoLop)
        {
            return _lopMonHocRepository.GetAllLopMonHoc(maHocKy, null, giangVienXacNhan, daPhanHoi, dongYMoLop);
        }

        public IEnumerable<QuanLyGiaoVuDAL.DTO.LopMonHocDTO> GetAllLopMonHocTheoGiangVien(Guid maGiangVien, bool? giangVienXacNhan, bool? daPhanHoi, bool? dongYMoLop)
        {
            return _lopMonHocRepository.GetAllLopMonHoc(null, maGiangVien, giangVienXacNhan, daPhanHoi, dongYMoLop);
        }

        public IEnumerable<QuanLyGiaoVuDAL.DTO.LopMonHocDTO> GetAllLopMonHocTheoGiangVienTrongHocKy(Guid maGiangVien, Guid maHocKy, bool? giangVienXacNhan, bool? daPhanHoi, bool? dongYMoLop)
        {
            return _lopMonHocRepository.GetAllLopMonHoc(maHocKy, maGiangVien,giangVienXacNhan, daPhanHoi, dongYMoLop);
        }


        public QuanLyGiaoVuDAL.DTO.LoaiLopDTO GetLoaiLopMonHocById(Guid id)
        {
            return _lopMonHocRepository.GetLoaiLopMonHocById(id);
        }

        public IEnumerable<QuanLyGiaoVuDAL.DTO.LoaiLopDTO> GetAllLoaiLopMonHoc()
        {
            return _lopMonHocRepository.GetAllLoaiLopMonHoc();
        }


        public QuanLyGiaoVuDAL.DTO.LopSinhVienDTO GetLopSinhVienById(Guid id)
        {
            return _lopMonHocRepository.GetLopSinhVienById(id);
        }

        public IEnumerable<QuanLyGiaoVuDAL.DTO.LopSinhVienDTO> GetAllLopSinhVien()
        {
            return _lopMonHocRepository.GetAllLopSinhVien();
        }


        public DangKyHocPhanTemplateModel LayThongTinDangKyHocPhanCuaLop(Guid maLopMonHoc)
        {
            var lopMonHoc = _lopMonHocRepository.GetLopMonHocById(maLopMonHoc);
            DangKyHocPhanTemplateModel templateModel = new DangKyHocPhanTemplateModel();
            templateModel.MaLop = lopMonHoc.MaLopDayDu;
            templateModel.MaMonHoc = lopMonHoc.MonHoc.MaMonHoc;
            templateModel.TenMonHoc = lopMonHoc.MonHoc.TenMonHoc;
            templateModel.MaGiangVien = string.Empty;
            templateModel.TenGiangVien = lopMonHoc.GiangVien1.GetTenVaHocVi();
            if (lopMonHoc.GiangVien2 != null)
                templateModel.TenGiangVien = templateModel.TenGiangVien + ", " + lopMonHoc.GiangVien2.GetTenVaHocVi();
            templateModel.SoSinhVienToiDa = lopMonHoc.SiSoToiDa ?? 0;
            templateModel.SiSo = lopMonHoc.SiSoThucTe ?? 0;
            templateModel.SoTCLyThuyet = lopMonHoc.MonHoc.SoTinChiLyThuyet;
            templateModel.SoTCThucHanh = lopMonHoc.MonHoc.TongSoTinChi - lopMonHoc.MonHoc.SoTinChiLyThuyet;
            templateModel.Khoa = lopMonHoc.LopSinhVien.TenLop;
            templateModel.GhiChu = lopMonHoc.GhiChu;
            templateModel.ThuPA1 = lopMonHoc.Thu;
            templateModel.TietPA1 = GetTietString(lopMonHoc.TietDau, lopMonHoc.TietSau);
            templateModel.ThuPA2 = 0;
            templateModel.TietPA2 = string.Empty;

            var phuongAnPhu =_dangKyGioDayRepository.GetAllDangKyGioday(null, maLopMonHoc, null, null, null, null).Where(pa => pa.DuocChon == false && pa.Huy == true);
            if (phuongAnPhu.Count() > 0)
            {
                var phuongAn2 = phuongAnPhu.First();
                templateModel.ThuPA2 = phuongAn2.Thu;
                templateModel.TietPA2 = phuongAn2.TietDauDTO.TietThu + " - " + phuongAn2.TietSauDTO.TietThu;
            }

            templateModel.HinhThucTH = lopMonHoc.LoaiLop == ThamSoConstantUtility.LOAILOPTHUCHANH2 ? "HT2" : "";
            templateModel.Buoi1 = lopMonHoc.Buoi1.HasValue ? (lopMonHoc.Buoi1.Value.Hour <= 12 ? "Sáng " + lopMonHoc.Buoi1.Value.ToShortDateString() : templateModel.Buoi1 = "Chiều " + lopMonHoc.Buoi1.Value.ToShortDateString()) : string.Empty;
            templateModel.Buoi2 = lopMonHoc.Buoi2.HasValue ? (lopMonHoc.Buoi2.Value.Hour <= 12 ? "Sáng " + lopMonHoc.Buoi2.Value.ToShortDateString() : templateModel.Buoi2 = "Chiều " + lopMonHoc.Buoi2.Value.ToShortDateString()) : string.Empty;
            templateModel.Buoi3 = lopMonHoc.Buoi3.HasValue ? (lopMonHoc.Buoi3.Value.Hour <= 12 ? "Sáng " + lopMonHoc.Buoi3.Value.ToShortDateString() : templateModel.Buoi3 = "Chiều " + lopMonHoc.Buoi3.Value.ToShortDateString()) : string.Empty;
            
            return templateModel;
        }

        public IEnumerable<QuanLyGiaoVuDAL.DTO.LopMonHocDTO> GetLopMonHocByIdList(string idList)
        {
            return _lopMonHocRepository.GetLopMonHocByIdList(idList);
        }


        public IEnumerable<LopMonHocDTO> GetAllLopMonHocTheoHocKy(Guid maHocKy, Guid? maMonHoc, bool? giangVienXacNhan, bool? daPhanHoi, bool? dongYMoLop)
        {
            if (maMonHoc.HasValue)
                return _lopMonHocRepository.GetAllLopMonHoc(maHocKy, null, giangVienXacNhan, daPhanHoi, dongYMoLop).Where(mh => mh.MaMonHoc == maMonHoc.Value);
            else
                return _lopMonHocRepository.GetAllLopMonHoc(maHocKy, null, giangVienXacNhan, daPhanHoi, dongYMoLop);
        }

        public string GetTietString(TietHocDTO tietDau, TietHocDTO tietSau)
        {
            if (tietDau == null || tietSau == null)
                return string.Empty;
            else
            {
                string result = string.Empty;
                for (int index = tietDau.TietThu; index <= tietSau.TietThu; index++)
                {
                    result += index.ToString();
                }
                return result;
            }
        }
    }
}